@@ -4,6 +4,7 @@ class SystemMailer < ActionMailer::Base |
||
4 | 4 |
def send_message(options) |
5 | 5 |
@groups = options[:groups] |
6 | 6 |
@headline = options[:headline] |
7 |
+ @body = options[:body] |
|
7 | 8 |
mail :to => options[:to], :subject => options[:subject] |
8 | 9 |
end |
9 | 10 |
end |
@@ -8,8 +8,14 @@ module Agents |
||
8 | 8 |
description <<-MD |
9 | 9 |
The EmailAgent sends any events it receives via email immediately. |
10 | 10 |
|
11 |
- The email will have a `subject` and an optional `headline` before listing the Events. If the Events' payloads |
|
12 |
- contain a `:message`, that will be highlighted, otherwise everything in their payloads will be shown. |
|
11 |
+ You can specify the email's subject line by providing a `subject` option, which can contain Liquid formatting. E.g., |
|
12 |
+ you could provide `"Huginn email"` to set a simple subject, or `{{subject}}` to use the `subject` key from the incoming Event. |
|
13 |
+ |
|
14 |
+ By default, the email body will contain an optional `headline`, followed by a listing of the Events' keys. |
|
15 |
+ |
|
16 |
+ You can customize the email body by including the optional `body` param. Like the `subject`, the `body` can be a simple message |
|
17 |
+ or a Liquid template. You could send only the Event's `some_text` field with a `body` set to `{{ some_text }}`. |
|
18 |
+ The body can contain simple HTML and will be sanitized. |
|
13 | 19 |
|
14 | 20 |
You can specify one or more `recipients` for the email, or skip the option in order to send the email to your |
15 | 21 |
account's default email address. |
@@ -29,7 +35,7 @@ module Agents |
||
29 | 35 |
incoming_events.each do |event| |
30 | 36 |
log "Sending digest mail to #{user.email} with event #{event.id}" |
31 | 37 |
recipients(event.payload).each do |recipient| |
32 |
- SystemMailer.delay.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :groups => [present(event.payload)]) |
|
38 |
+ SystemMailer.delay.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :body => interpolated(event)['body'], :groups => [present(event.payload)]) |
|
33 | 39 |
end |
34 | 40 |
end |
35 | 41 |
end |
@@ -4,18 +4,22 @@ |
||
4 | 4 |
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> |
5 | 5 |
</head> |
6 | 6 |
<body> |
7 |
- <% if @headline %> |
|
8 |
- <h1><%= sanitize @headline %></h1> |
|
9 |
- <% end %> |
|
10 |
- <% @groups.each do |group| %> |
|
11 |
- <div style='margin-bottom: 10px;'> |
|
12 |
- <div><%= sanitize group[:title] %></div> |
|
13 |
- <% group[:entries].each do |entry| %> |
|
14 |
- <div style='margin-left: 10px;'> |
|
15 |
- <%= sanitize entry %> |
|
16 |
- </div> |
|
17 |
- <% end %> |
|
18 |
- </div> |
|
7 |
+ <% if @body.present? %> |
|
8 |
+ <%= sanitize @body %> |
|
9 |
+ <% else %> |
|
10 |
+ <% if @headline %> |
|
11 |
+ <h1><%= sanitize @headline %></h1> |
|
12 |
+ <% end %> |
|
13 |
+ <% @groups.each do |group| %> |
|
14 |
+ <div style='margin-bottom: 10px;'> |
|
15 |
+ <div><%= sanitize group[:title] %></div> |
|
16 |
+ <% group[:entries].each do |entry| %> |
|
17 |
+ <div style='margin-left: 10px;'> |
|
18 |
+ <%= sanitize entry %> |
|
19 |
+ </div> |
|
20 |
+ <% end %> |
|
21 |
+ </div> |
|
22 |
+ <% end %> |
|
19 | 23 |
<% end %> |
20 | 24 |
</body> |
21 | 25 |
</html> |
@@ -1,7 +1,10 @@ |
||
1 |
+<% if @body.present? %><%= sanitize @body -%> |
|
2 |
+<% else -%> |
|
1 | 3 |
<% if @headline %><%= @headline %> |
2 | 4 |
|
3 | 5 |
<% end %><% @groups.each do |group| %><%= group[:title] %> |
4 | 6 |
<% group[:entries].each do |entry| %> <%= entry %> |
5 | 7 |
<% end %> |
6 | 8 |
|
9 |
+<% end %> |
|
7 | 10 |
<% end %> |
@@ -11,6 +11,7 @@ describe Agents::EmailAgent do |
||
11 | 11 |
@checker = Agents::EmailAgent.new(:name => "something", :options => { :expected_receive_period_in_days => "2", :subject => "something interesting" }) |
12 | 12 |
@checker.user = users(:bob) |
13 | 13 |
@checker.save! |
14 |
+ expect(ActionMailer::Base.deliveries).to eq([]) |
|
14 | 15 |
end |
15 | 16 |
|
16 | 17 |
after do |
@@ -19,11 +20,9 @@ describe Agents::EmailAgent do |
||
19 | 20 |
|
20 | 21 |
describe "#receive" do |
21 | 22 |
it "immediately sends any payloads it receives" do |
22 |
- expect(ActionMailer::Base.deliveries).to eq([]) |
|
23 |
- |
|
24 | 23 |
event1 = Event.new |
25 | 24 |
event1.agent = agents(:bob_rain_notifier_agent) |
26 |
- event1.payload = { :data => "Something you should know about" } |
|
25 |
+ event1.payload = { :message => "hi!", :data => "Something you should know about" } |
|
27 | 26 |
event1.save! |
28 | 27 |
|
29 | 28 |
event2 = Event.new |
@@ -38,7 +37,7 @@ describe Agents::EmailAgent do |
||
38 | 37 |
expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"]) |
39 | 38 |
expect(ActionMailer::Base.deliveries.last.subject).to eq("something interesting") |
40 | 39 |
expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to eq("Event\n data: Something else you should know about") |
41 |
- expect(get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip).to eq("Event\n data: Something you should know about") |
|
40 |
+ expect(get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip).to eq("hi!\n data: Something you should know about") |
|
42 | 41 |
end |
43 | 42 |
|
44 | 43 |
it "can receive complex events and send them on" do |
@@ -56,5 +55,25 @@ describe Agents::EmailAgent do |
||
56 | 55 |
expect(plain_email_text).to match(/avehumidity/) |
57 | 56 |
expect(html_email_text).to match(/avehumidity/) |
58 | 57 |
end |
58 |
+ |
|
59 |
+ it "can take body option for selecting the resulting email's body" do |
|
60 |
+ @checker.update_attributes :options => @checker.options.merge({ |
|
61 |
+ 'subject' => '{{foo.subject}}', |
|
62 |
+ 'body' => '{{some_html}}' |
|
63 |
+ }) |
|
64 |
+ |
|
65 |
+ event = Event.new |
|
66 |
+ event.agent = agents(:bob_rain_notifier_agent) |
|
67 |
+ event.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<strong>rain!</strong>" } |
|
68 |
+ event.save! |
|
69 |
+ |
|
70 |
+ Agents::EmailAgent.async_receive(@checker.id, [event.id]) |
|
71 |
+ |
|
72 |
+ expect(ActionMailer::Base.deliveries.count).to eq(1) |
|
73 |
+ expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"]) |
|
74 |
+ expect(ActionMailer::Base.deliveries.last.subject).to eq("Something you should know about") |
|
75 |
+ expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to match(/\A\s*<strong>rain\!<\/strong>\s*\z/) |
|
76 |
+ expect(get_message_part(ActionMailer::Base.deliveries.last, /html/).strip).to match(/<body>\s*<strong>rain\!<\/strong>\s*<\/body>/) |
|
77 |
+ end |
|
59 | 78 |
end |
60 | 79 |
end |